package com.cskaoyan.mall.search.service.impl;

import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.search.api.ProductApi;
import com.cskaoyan.mall.search.consts.SearchRetCode;
import com.cskaoyan.mall.search.converter.ProductConverter;
import com.cskaoyan.mall.search.entity.ItemDocument;
import com.cskaoyan.mall.search.exception.SearchException;
import com.cskaoyan.mall.search.repository.ProductRepository;
import com.cskaoyan.mall.search.service.InitDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 牟治宇
 * @description 初始化实现类
 * @date: 2022/8/6 12:15
 */
@Service
@Slf4j
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    ProductApi productApi;
    @Autowired
    ProductConverter productConverter;

    @Autowired
    ProductRepository productRepository;

    @Override
    public void initItems() {
        try {
            AllItemResponse allProductItem = productApi.getAllProductItem();
            List<ItemDto> allItems = allProductItem.getAllItems();
            List<ItemDocument> itemDocuments = productConverter.items2Documents(allItems);

            productRepository.saveAll(itemDocuments);
        } catch (Exception e) {
            log.error("InitDataServiceImpl.initItems Occur Exception :" + e);
            e.printStackTrace();
        }

    }
}
